package com.tulele.vtool.handler;

import com.google.common.collect.Maps;
import com.tulele.vtool.model.combinedEntity.BBeginnerSetting;
import com.tulele.vtool.model.combinedEntity.PlatformArticleType;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.BPlatformBeginnerArticleTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/beginner")
public class BeginnerArticleInfoHandler {
    @Autowired
    private BPlatformBeginnerArticleTypeService bPlatformBeginnerArticleTypeService;

    /**
     * 新手学堂文章类目
     * @param identifying 公众号标识
     * @return
     */
    @GetMapping(value = "/articleType", produces = "application/json;charset=UTF-8")
    public RespData articleType(@RequestHeader("identifying") String identifying) {
        // 1、页面文章类目
        List<PlatformArticleType> articleType = bPlatformBeginnerArticleTypeService.getPlatformArticleType(identifying);

        // 2、页面文章列表
        if (CollectionUtils.isNotEmpty(articleType)) {
            articleType.forEach((info) -> {
                info.setArticleList(bPlatformBeginnerArticleTypeService.getPlatformArticleInfo(identifying, info.getId(), info.getDisplayCount()));
            });
        }

        // 3、公众号新手学堂相关配置信息
        BBeginnerSetting beginnerSetting = bPlatformBeginnerArticleTypeService.getBeginnerPageInfo(identifying);

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(10);
        resultMap.put("articleType", articleType);
        resultMap.put("beginnerSetting", beginnerSetting);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        respData.setData(resultMap);
        return respData;
    }

    /**
     * 新手学堂用户阅读记录统计
     * @param identifying
     * @return
     */
    @PostMapping(value = "/lookArticle", produces = "application/json;charset=UTF-8")
    public RespData lookArticle(@RequestHeader("identifying") String identifying) {
        // 加锁
        synchronized (this) {
            bPlatformBeginnerArticleTypeService.addBeginnerLookArticle(identifying);
        }

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }
}
